/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * <p>Project: javase2203 - T15
 * <p>Powered by webrx On 2022-08-09 15:07:05
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T15 {
    List<String> list = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);

    void add() {
        System.out.println("add");

        try {
            for (int i = 0; i < 20; i++) {
                Thread.sleep(1000);
                String it = "Item - " + i;
                list.add(it);
                System.out.println(it);
                if (list.size() == 5) {
                    latch.countDown();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("add end");
    }

    void check() {
        System.out.println("检查线程启动");
        System.out.println("统计中...");
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("统计到5个了，检查结束");
    }

    public static void main(String[] args) {
        T15 t = new T15();

        new Thread(t::add).start();
        new Thread(t::check).start();
    }
}
